﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

namespace Aspfm.Main
{
    public abstract class IMainController
    {
        System.Web.HttpContext _Context = null;
        UserIdentity _User = null;
        AspfmModel _RequestDynObj = null;
        public void Init(System.Web.HttpContext context, UserIdentity user, AspfmModel requestDynObj)
        {
            _Context = context;
            _User = user;
            _RequestDynObj = requestDynObj;
        }
        //获取页面传过来的文件流
        public System.Web.HttpFileCollection GetHttpFiles()
        {
            if (_Context != null)
            {
                return _Context.Request.Files;
            }
            return null;
        }
        //当前aspfm系统登录信息
        public UserIdentity User { get { if (_User == null) { _User = new UserIdentity(); } return _User; } }
        //返回页面传过来的所有参数
        public AspfmModel RequestDynObj { get { if (_RequestDynObj == null) { _RequestDynObj = new AspfmModel(); } return _RequestDynObj; } }
        //返回服务器站点完整目录
        public string _Path { get { return BaseAshx.CurrentPath(); } }
        //是否是Post
        public bool IsHttpPost { get { return GetHttpIsPost(); } }
        //是否是Post
        public bool GetHttpIsPost()
        {
            return BaseAshx.GetHttpIsPost(_Context);
        }
        //返回页面所在站点的完整目录
        public string GetLocalPath()
        {
            return BaseAshx.GetLocalPath(_Context, RequestDynObj);
        }

        //必须继承重写的抽象函数，对继承者来说，这是执行分发器
        public abstract void MvcController(string active);

        //后台处理完成之后，可以这此函数返回JSON对象到JS中。
        public void ReturnObj(object obj)
        {
            if (obj != null)
            {
                string jsonResult = WebResultList.SerializeObject(obj);
                if (_Context != null)
                {
                    _Context.Response.AddHeader("Server", "Aspfm Server V0.0.1");
                    _Context.Response.AddHeader("Access-Control-Allow-Origin", "*");
                    _Context.Response.ContentType = "application/json";
                    _Context.Response.Write(jsonResult);
                    _Context.Response.End();
                }
            }
        }
        //后台处理完成之后，可以这此函数返回字符串到JS中。
        public void ReturnHtml(string HtmlStr)
        {
            BaseAshx.ReturnHtml(HtmlStr, _Context);
        }
        //后台处理完成之后，可以这此函数返回文件流，isDownload表示是否通知浏览器下载
        public void ReturnFile(string FileName, bool isDownload = true)
        {
            if (System.IO.File.Exists(FileName))
            {
                string fileName = System.IO.Path.GetFileName(FileName);//文件名
                //以字符流的形式下载文件
                System.IO.FileStream fs = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                string ContentType = BaseAshx.GetContentType(System.IO.Path.GetExtension(fileName));
                if (_Context != null)
                {
                    _Context.Response.ContentType = ContentType;
                    if (ContentType == "application/octet-stream" || isDownload)  //通知浏览器下载文件而不是打开  
                    {
                        _Context.Response.AddHeader("Content-Disposition", "attachment;  filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    }
                    _Context.Response.BinaryWrite(bytes);
                    _Context.Response.Flush();
                    _Context.Response.End();
                }
            }
            else
            {
                ReturnHtml("未上传文件!!" + FileName);
            }
        }
    }
}
